package idv.nightpig.lab01.web;

import idv.nightpig.lab01.domain.Blog;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import com.google.inject.name.Named;
import com.google.sitebricks.At;
import com.google.sitebricks.http.Get;
import com.google.sitebricks.http.Post;
import com.google.sitebricks.http.Select;

@At("/blogs")
public class BlogCollection {

    @At("/blog/:id")
    @Select("action")
    public static class BlogElement {

        private Blog blog;

        @Get
        public void doGet(@Named("id") String id) {
            blog = BLOGS.get(id);
        }

        public Blog getBlog() {
            return blog;
        }

        public void setBlog(Blog blog) {
            this.blog = blog;
        }

    }

    private final static Map<String, Blog> BLOGS = new TreeMap<String, Blog>();

    private Blog blog = new Blog();

    @Post
    public BlogCollection doPost() {
        blog.setId(UUID.randomUUID().toString());
        BLOGS.put(blog.getId(), blog);
        return new BlogCollection();
    }

    public Blog getBlog() {
        return blog;
    }

    public Collection<Blog> getBlogs() {
        return BLOGS.values();
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

}
